<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="评论用户" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择评论用户">
          <el-option
            v-for="item in commentList2"
            :key="item.userId"
            :label="item.user? item.user.userName:''"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="评论订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入评论订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="评分" prop="score">
        <el-input
          v-model="queryParams.score"
          placeholder="请输入评分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复时间" prop="replyTime">
        <el-date-picker
          clearable
          v-model="queryParams.replyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择回复时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="评论时间" prop="commentTime">
        <el-date-picker
          clearable
          v-model="queryParams.commentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择评论时间"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="评论标签" prop="commentTag">
        <el-input
          v-model="queryParams.commentTag"
          placeholder="请输入评论标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否删除 0正常  1删除" prop="isDel">
				<el-input v-model="queryParams.isDel" placeholder="请输入是否删除 0正常  1删除" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['system:comment:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['system:comment:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['system:comment:remove']">删除</el-button>
			</el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:comment:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="commentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评论编号" align="center" prop="id" />
      <el-table-column label="评论用户" align="center">
        <template slot-scope="scope">{{ scope.row.user.userName }}</template>
      </el-table-column>
      <el-table-column label="评论订单ID" align="center" prop="orderId" >
        <template slot-scope="scope"><p style="cursor:pointer" @click="handleorderView(scope.row)">{{ scope.row.orderId }}</p></template>
      </el-table-column>
      <el-table-column label="评论内容" align="center" prop="content" />
      <el-table-column
        label="评论图片"
        align="center"
        prop="images"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="平台回复" align="center" prop="reply" />
      <el-table-column
        label="回复时间"
        align="center"
        prop="replyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.replyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="评论时间"
        align="center"
        prop="commentTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commentTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="评论标签" align="center" prop="commentTag" /> -->
      <el-table-column label="审核结果" align="center" prop="isExamine">
        <template slot-scope="scope">
          <el-tag type="danger" size="mini" v-if="scope.row.isExamine == 0"
            >未审核</el-tag
          >
          <el-tag type="success" size="mini" v-if="scope.row.isExamine == 1"
            >通过</el-tag
          >
          <el-tag type="warning" size="mini" v-if="scope.row.isExamine == 2"
            >未通过</el-tag
          >
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否删除 0正常  1删除" align="center" prop="isDel" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.isExamine == 0 ? 'el-icon-edit' : 'el-icon-view'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:comment:edit']"
            >{{ scope.row.isExamine == 0 ? "审核" : "查看" }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:comment:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家政商品评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="评论用户">
          <el-input
            v-model="user.userName"
            placeholder="请输入评论用户ID"
            disabled
          />
        </el-form-item>
        <el-form-item label="评论订单ID">
          <el-input
            v-model="form.orderId"
            placeholder="请输入评论订单ID"
            disabled
          />
        </el-form-item>
        <el-form-item label="评论内容">
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入内容"
            disabled
          />
        </el-form-item>
        <el-form-item label="评论图片" prop="images">
          <!-- <image-upload v-model="form.images" /> -->
          <template v-if="form.images">
            <image-preview
              v-for="(item, index) in form.images.split(',')"
              :src="api_host + item"
              :width="100"
              :height="100"
            />
          </template>
        </el-form-item>
        <el-form-item label="服务质量" prop="stepScore">
          <el-rate
            v-model="stepScore2[0]"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-form-item>
        <el-form-item label="服务态度" prop="stepScore">
          <el-rate
            v-model="stepScore2[1]"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-form-item>
        <el-form-item label="按时上门" prop="stepScore">
          <el-rate
            v-model="stepScore2[2]"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-form-item>
        <el-form-item label="客服响应" prop="stepScore">
          <el-rate
            v-model="stepScore2[3]"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-form-item>
        <el-form-item label="符合描述" prop="stepScore">
          <el-rate
            v-model="stepScore2[4]"
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>
        </el-form-item>
        <el-form-item label="平台回复" prop="reply">
          <el-input
            v-model="form.reply"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="审核结果">
          <el-radio-group v-model="form.isExamine">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="回复时间" prop="replyTime">
					<el-date-picker clearable v-model="form.replyTime" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择回复时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="评论时间" prop="commentTime">
					<el-date-picker clearable v-model="form.commentTime" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择评论时间">
					</el-date-picker>
				</el-form-item> -->
        <!-- <el-form-item label="评论标签" prop="commentTag">
					<el-input v-model="form.commentTag" placeholder="请输入评论标签" />
				</el-form-item>
				<el-form-item label="是否删除 0正常  1删除" prop="isDel">
					<el-input v-model="form.isDel" placeholder="请输入是否删除 0正常  1删除" />
				</el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="isShow == 0">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="订单详情" :visible.sync="open2" append-to-body>
      <el-form ref="form" :model="form2" :rules="rules" label-width="80px">
        <el-steps v-if="form2.orderStatus<11" :space="200" :active="form2.orderStatus" finish-status="success">
          <el-step title="未支付"></el-step>
          <el-step title="待出发"></el-step>
          <el-step title="待商户确认"></el-step>
          <el-step title="待用户确认"></el-step>
          <el-step v-if="form2.orderStatus==4" title="已完成" status="success"></el-step>
          <el-step v-else title="已完成"></el-step>
        </el-steps>
        <el-tag v-else type="info">订单已取消</el-tag>
        <div style="display: flex; justify-content: space-between" class="mt10">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>订单信息</span>
            </div>
            <div class="item">
              <span>订单号:{{ form2.orderCode }}</span>
            </div>
            <div class="item">
              <span>订单状态:{{ orderStatus[form2.orderStatus] }}</span>
            </div>
            <div class="item">
              <span>支付时间:{{ form2.payTime }}</span>
            </div>
            <div class="item">
              <span>订单金额:{{ form2.orderPrice }}</span>
            </div>
          </el-card>

          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>用户信息</span>
            </div>
            <div class="item">
              <span>联系人:{{ userAddr.realName }}</span>
            </div>
            <div class="item">
              <span>电话:{{ userAddr.phone }}</span>
            </div>
            <div class="item">
              <span
                >地址:{{ userAddr.userAddr }} - {{ userAddr.houseNumber }}</span
              >
            </div>
            <div class="item">
              <span>期望时间:{{ form2.timePeriod }}</span>
            </div>
          </el-card>
        </div>
        <div style="display: flex; justify-content: space-between" class="mt10">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>商品信息</span>
            </div>
            <div v-for="(item,index) in orderDetails" :key="index">
              <div class="item">
                <span>商品名称:{{ item.name }}</span>
              </div>
              <div class="item">
                <span>商品价格:{{ item.price }}</span>
              </div>
              <div class="item">
                <span>购买数量:{{ item.buyCount }}</span>
              </div>
              <div class="item">
                <span>总计金额:{{ item.price*item.buyCount }}</span>
              </div>
            </div>
          </el-card>
          <el-card class="box-card" v-if="provider!=null">
            <div slot="header" class="clearfix">
              <span>商家信息</span>
            </div>
            <div class="item">
              <span>商家名称:{{ provider.account }}</span>
            </div>
            <div class="item">
              <span>预计上门时间:{{ form2.estimateTime }}</span>
            </div>
            <div class="item">
              <span>联系人姓名:{{ provider.provName }}</span>
            </div>
            <div class="item">
              <span>联系人电话:{{ provider.phone }}</span>
            </div>
          </el-card>
        </div>
      </el-form>
      
      <div class="" v-if="orderAppend != null">
        <el-card style="margin-top: 20px">
          <div slot="header" class="clearfix">
            <span>追加服务</span>
          </div>
          <div>
            <span>追加订单号:{{ orderAppend.appendCode }}</span>
          </div>
          <div class="mt10">
            <span>追加金额:￥{{ orderAppend.price }}</span>
          </div>
          <div class="mt10">
            <span>追加内容：</span>
          </div>
          <div class="mt10">
            <span>{{ orderAppend.describe }}</span>
          </div>
          <div class="mt10" v-if="form2.sceneImages">
            <image-preview
              v-for="(item, index) in form2.sceneImages.split(',')"
              :src="api_host + item"
              :width="100"
              :height="100"
            />
          </div>
        </el-card>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('1')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listComment,
  getComment,
  delComment,
  addComment,
  updateComment,
} from "@/api/system/comment";
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  getProvider,
} from "@/api/system/order";


export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家政商品评论表格数据
      commentList: [],
      commentList2:[], //评价用户列表
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2:false,
      orderStatus: {
        0: "未支付",
        1: "待出发",
        2: "待商户确认",
        3: "待用户确认",
        4: "已完成",
        11: "已取消",
        22: "售后",
        33: "售后完成",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderId: null,
        content: null,
        images: null,
        score: null,
        reply: null,
        replyTime: null,
        commentTime: null,
        commentTag: null,
        isExamine: null,
        isDel: null,
        stepScore: null,
        user:null
      },
      // 表单参数
      form: {},
      form2:{},
      userAddr:{},
      orderAppend:{},
      orderDetails:[],
      provider:{},
      user:{},
      stepScore2: [],
      // 表单校验
      rules: {
        userId: [
          {
            required: true,
            message: "评论用户ID不能为空",
            trigger: "blur",
          },
        ],
        orderId: [
          {
            required: true,
            message: "评论订单ID不能为空",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "评论内容不能为空",
            trigger: "blur",
          },
        ],
        score: [
          {
            required: true,
            message: "评分不能为空",
            trigger: "blur",
          },
        ],
        isExamine: [
          {
            required: true,
            message: "是否审核 0未审核  1通过 2不通过不能为空",
            trigger: "change",
          },
        ],
        isDel: [
          {
            required: true,
            message: "是否删除 0正常  1删除不能为空",
            trigger: "blur",
          },
        ],
      },
      isShow: 0,
    };
  },
  created() {
    this.getList();
    this.getList2()
  },
  methods: {
    /** 查询家政商品评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then((response) => {
        this.commentList = response.rows;
        // this.commentList2 = this.distinct(response.rows,"userId")
        this.total = response.total;
        this.loading = false;
      });
    },
    getList2() {
      this.loading = true;
      listComment().then((response) => {
        this.commentList2 = this.distinct(response.rows,"userId")
        console.log(this.commentList2,222111);
        
        this.loading = false;
      });
    },
    //去重
    distinct(arr, key) {
      let newobj = {}; 
      arr = arr .reduce((preVal, curVal) => {
          newobj[curVal[key]] ? '' : newobj[curVal[key]] = preVal.push(curVal); 
          return preVal 
      }, []);
      return arr;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderId: null,
        content: null,
        images: null,
        score: null,
        reply: null,
        replyTime: null,
        commentTime: null,
        commentTag: null,
        isExamine: null,
        isDel: null,
        stepScore: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加家政商品评论";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getComment(id).then((response) => {
        this.form = response.data;
        this.user = response.data.user
        this.form.isExamine = response.data.isExamine.toString();
        this.isShow = response.data.isExamine;
        // this.form.stepScore = response.data.stepScore.split(',') || []
        this.stepScore2 = response.data.stepScore.split(",");
        this.open = true;
        this.title = "审核家政商品评论";
      });
    },
    //根据订单id查看详情
    handleorderView(row){
      const id = row.orderId
      getOrder(id).then((response)=>{
        console.log(response.data);
        this.form2 = response.data;
        this.userAddr = response.data.userAddr;
        this.orderAppend = response.data.orderAppend;
        this.orderDetails = response.data.orderDetails
        this.provider = response.data.provider
        this.open2 = true;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateComment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除家政商品评论编号为"' + ids + '"的数据项？')
        .then(function () {
          return delComment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/comment/export",
        {
          ...this.queryParams,
        },
        `comment_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scope>
.el-rate {
  line-height: 2;
}
.box-card {
  width: 450px;
}
.item {
  margin-bottom: 18px;
}
</style>
